<?php
use Illuminate\Support\Facades\Route;

if(!function_exists('route_class')){
    function route_class() {
        return str_replace('.','-',Route::currentRouteName());
    }
}

if(!function_exists('category_nav_active')){
    function category_nav_active($category_id) {
        return active_class(if_route('categories.show') && if_route_param('category',$category_id));
    }
}
if(!function_exists('make_excerpt')){
    // \r是回车，英文是Carriage return，作用：使光标到行首
    //
    // \n是换行，英文是New line/line feed，作用：使光标下移一行
    function make_excerpt($value,$length = 200) {
        $excerpt = trim(str_replace('/\r\n|\r|\n+/',' ',strip_tags($value)));
        return str()->limit($excerpt,$length);
    }
}
if(!function_exists('model_plural_name')){
    function model_plural_name($model){
        $full_model_name = get_class($model);
        $class_name = class_basename($full_model_name);
        $snake_case_name = str()->snake($class_name);
        return str()->plural($snake_case_name);
    }
}
if(!function_exists('model_link')){
    function model_link($title,$model,$prefix='') {
        $model_name = model_plural_name($model);
        $prefix = $prefix ? "/$prefix/" : '/';
        $url = config('app.url').$prefix.$model_name.'/'.$model->id;
        return '<a href="'.$url.'" target="_blank">'.$title.'</a>';
    }
}
if(!function_exists('model_admin_link')){
    function model_admin_link($title,$model){
        return model_link($title,$model,'admin');
    }
}
